package com.acme.domain.bank;

public class EmailSender implements Runnable {

	private Queue queue;
	private boolean todo;
	Thread t;

	public EmailSender(Queue queue) {
		this.queue = queue;
		todo = true;
		t = new Thread(this);
	}

	//this method for point "Thread sleep(1000)"
//	@Override
//	public void run() {
//		while(todo) {
//			synchronized(queue) {
//				while(queue.getSize() > 0) {
//					queue.sendEmail();
//					try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}
//			}
//		}
//	}
	
	@Override
	public void run() {
		
		while(todo) {
			synchronized (queue) {
				while(queue.getSize() > 0) {
					try {
						queue.sendEmail();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
	}
	
	public boolean isTodo() {
		return todo;
	}

	public void setTodo(boolean todo) {
		this.todo = todo;
	}

}
